package sdu.attendancechecker.servlet.teacher;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sdu.attendancechecker.dto.Teacher;
import sdu.attendancechecker.models.TeacherModel;
import sdu.attendancechecker.models.SubjectModel;
import sdu.attendancechecker.services.TeacherService;

public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher t = (Teacher) session.getAttribute("teacher");
		if (t == null) {
			response.sendRedirect("teacher/login");
			return;
		}
		response.sendRedirect("teacher/main");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {		
		if (request.getParameter("emailorid") != null
				&& request.getParameter("password") != null) {
			String login = (String) request.getParameter("emailorid");
			String password = (String) request.getParameter("password");
			Teacher teacher = TeacherModel.getByLoginAndPassword("email",
					login, password);
			if (teacher == null) {
				teacher = TeacherModel.getByLoginAndPassword("id", login,
						password);
			}
			if (teacher == null) {
				response.getWriter().println("Invalid login or password");
				return;
			}
			HttpSession session = request.getSession();
			teacher.setSubjects(SubjectModel.getSubjects(teacher.getId()));
			session.setAttribute("teacher", teacher);
			doGet(request, response);
			return;
		}

		String command = request.getParameter("command");
		if (command.equals("logout")) {
			request.getSession().removeAttribute("teacher");
			response.getWriter().print(0);
			return;
		}

		if (command.equals("changepassword")) {
			System.out.println("kirdi");
			String current = request.getParameter("current");
			String newpass = request.getParameter("new");
			String confpass = request.getParameter("conf");
			Teacher teacher = (Teacher) request.getSession().getAttribute(
					"teacher");
			if (TeacherModel.getByLoginAndPassword("id", teacher.getId() + "",
					current) == null) {
				response.getWriter().print(1);
				return;
			}
			if (!newpass.equals(confpass)) {
				response.getWriter().print(2);
				return;
			}
			if (TeacherModel.updatePasswordByEmail(teacher.getEmail(), newpass)) {
				response.getWriter().print(0);
				return;
			} else {
				response.getWriter().print(3);
				return;
			}
		}

		if (command.equals("changeemail")) {
			String newEmail = request.getParameter("email");
			Teacher teacher = (Teacher) request.getSession().getAttribute(
					"teacher");
			if (TeacherModel.updateEmailById(newEmail, teacher.getId())) {
				response.getWriter().print(0);
				return;
			} else {
				response.getWriter().print(1);
				return;
			}
		}
		
		if (command.equals("sort")) {
			String sortby = request.getParameter("sortby");
			//response.getWriter().println(TeacherService.getSortedSubjects(sortby).toString());
			return;
		}
	}
}
